/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.b;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.annotation.Inner;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.UserTypeEnum;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.*;
import com.ykht.proj.coupon.vo.CnCouponDetailReq;
import com.ykht.proj.coupon.vo.CnCouponDetailResp;
import com.ykht.proj.coupon.vo.CnCouponVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 优惠券表
 *
 * @author proj code generator
 * @date 2020-11-16 17:00:20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/b/cncoupon")
@Api(value = "cncoupon", tags = "b端优惠券表管理")
@Slf4j
public class BnCouponController {

	private final CnCouponService cnCouponService;

	private final CnCompanyService cnCompanyService;

	private final CnBrandService cnBrandService;

	private final CnShopService cnShopService;

	private final CnCouponLogService cnCouponLogService;

	/**
	 * 分页查询
	 *
	 * @param page       分页对象
	 * @param cnCouponVo 优惠券表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	public R getCnCouponPage(Page page, CnCouponVo cnCouponVo) {
		Integer userType = SecurityUtils.getUser().getUserType();
		Long sjId = SecurityUtils.getSjId();
		if (userType.equals(UserTypeEnum.company.getValue())) {
			cnCouponVo.setCompanyId(sjId);
		}
		if (userType.equals(UserTypeEnum.brand.getValue())) {
			cnCouponVo.setBrandIds(sjId.toString());
		}
		if (userType.equals(UserTypeEnum.shop.getValue())) {
			CnShop cnShop = cnShopService.getById(sjId);
			cnCouponVo.setShopId(cnShop.getId());
			cnCouponVo.setBrandId(cnShop.getBrandId());
		}
		return cnCouponService.findPage(page, cnCouponVo);
	}

	/**
	 * 通过id查询优惠券表
	 *
	 * @return R
	 */
	@ApiOperation(value = "优惠券详情")
	@PostMapping("detail")
	@Inner(false)
	public R<CnCouponDetailResp> getById(@RequestBody CnCouponDetailReq req) {
		try {
			return cnCouponLogService.getCouponById(req.getId());
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed();
		}
	}

	/**
	 * 通过id查询优惠券表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(cnCouponService.getById(id));
	}

	/**
	 * 新增优惠券表
	 *
	 * @param cnCouponVo 优惠券表
	 * @return R
	 */
	@ApiOperation(value = "新增优惠券表", notes = "新增优惠券表")
	@PostMapping
	public R save(@RequestBody CnCouponVo cnCouponVo) {
		return cnCouponService.insertCnCoupon(cnCouponVo);
	}


	/**
	 * 优惠券 排序
	 * @param couponList 优惠券表
	 * @return R
	 */
	@ApiOperation(value = "优惠券 排序", notes = "优惠券 排序")
	@PostMapping("updateBySort")
	public R updateBySort(@RequestBody List<CnCoupon> couponList) {
		log.info("优惠券 排序:{}", couponList);
		couponList.stream().forEach(itme ->{
			itme.setUpdateUser(SecurityUtils.getUserId());
			itme.setUpdateTime(LocalDateTime.now());
		});
		return R.ok(cnCouponService.updateBatchById(couponList));

	}

	/**
	 * 通过id删除优惠券表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除优惠券表", notes = "通过id删除优惠券表")
	@DeleteMapping("/{id}")
	public R removeById(@PathVariable Long id) {
		return R.ok(cnCouponService.removeById(id));
	}

	/**
	 * 上下架
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "上下架", notes = "上下架")
	@GetMapping("upOrLow")
	public R upOrLow(Long id) {
		return cnCouponService.upOrLow(id);
	}

	/**
	 * 获取门店
	 */
	@ApiOperation(value = "获取门店", notes = "获取门店")
	@GetMapping("getShop")
	public R getShop(Long brandId) {
		return cnCouponService.getShop(brandId);
	}

	/**
	 * 获取品牌
	 *
	 * @return
	 */
	@ApiOperation(value = "获取品牌", notes = "获取品牌")
	@GetMapping("getBrand")
	public R getBrand() {
		return cnCouponService.getBrand();
	}

	/**
	 * 优惠券查询
	 *
	 * @param brandId 门店id
	 * @return
	 */
	@ApiOperation(value = "优惠券查询", notes = "优惠券查询")
	@GetMapping("/findCouponByShopId")
	public R findCouponByShopId(Long brandId, String couponName) {
		List<CnCouponVo> result = cnBrandService.findCouponByShopId(brandId, couponName);
		return R.ok(result);
	}
}
